﻿using System;
using System.Threading;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;
using SecretLabs.NETMF.Hardware;
using SecretLabs.NETMF.Hardware.Netduino;

namespace MorseCode
{
    public class Program
    {
        // Please change these as you wish!
        static String UncodedMessage = "Hoverbear"; // You can set your phrase here!
        static int Rate = 13 * 60; // I found this to be an acceptable value, but I can't read morse.

        // These you probably don't need to change.
        static OutputPort LED = new OutputPort(Pins.ONBOARD_LED, false);
        static MorseCode Morse;

        /// <summary>
        /// Initialize our attached devices and setup an interrupt.
        /// </summary>
        public static void Main()
        {
            // Set up our interupt port. We're using event based because I think it's badass.
            InterruptPort Toggle = new InterruptPort(Pins.ONBOARD_SW1, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeLow);
            Toggle.OnInterrupt += new NativeEventHandler(Toggle_OnInterrupt);

            Morse = new MorseCode(UncodedMessage); // Setting up the MorseCode
            Thread.Sleep(Timeout.Infinite); // Wait for interupt.
        }

        /// <summary>
        /// Sends the message when button is pressed.
        /// </summary>
        /// <param name="Data1">Unused.</param>
        /// <param name="Data2">Unused.</param>
        /// <param name="Time">Unused.</param>
        public static void Toggle_OnInterrupt(uint Data1, uint Data2, DateTime Time)
        {
            SendMorse();
        }

        /// <summary>
        /// Sends the Morse to the LED and pauses when needed.
        /// </summary>
        private static void SendMorse()
        {
            for (int step = 0; step < Morse.SignalCount(); step++)
            {
                switch (Morse.GetSignal(step))
                {
                    case 1:
                        Debug.Print("Norse: Dot happening!");
                        LED.Write(true);
                        Thread.Sleep(Rate); // Wait 1 tick!
                        LED.Write(false);
                        Thread.Sleep(Rate); // 1 tick between signals, always!
                        break;
                    case 2:
                        Debug.Print("Morse: Dash Happening!");
                        LED.Write(true);
                        Thread.Sleep(Rate * 3); // Wait 3 ticks.
                        LED.Write(false);
                        Thread.Sleep(Rate); // 1 tick between signals, always!
                        break;
                    case 3:
                        Debug.Print("Morse: Letter Break Happening!");
                        Thread.Sleep(Rate * 2); // We have 1 rate sleep from the last char.
                        break;
                    case 4:
                        Debug.Print("Morse: Word Break Happening!");
                        Thread.Sleep(Rate * 6); // We have 1 rate sleep from the last char.
                        break;
                    default:
                        Debug.Print("Morse: There was an error!");
                        break;
                }
            }
        }
    }
}